package com.platform.enums;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.Getter;

import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

/**
 * Description: 法人证件类型
 *
 * @author libin created on 2019/9/25
 */
public enum LegIdType {
    /**
     * 证件类型
     */
    SHXYM("L1", "id_shxym", "统一社会信用代码"),
    GSZC("L2", "id_gszc", "工商注册号"),
    ZZJG("L3", "id_zzjg", "组织机构代码"),
    SWDJ("L4", "id_swdj", "税务登记号"),
    SYDW("L5", "id_sydw", "事业单位证书号"),
    SHZZ("L6", "id_shzz", "社会组织登记号"),
    OTHER("99", "id_other", "其他");
    /**
     * 编码-对应字典编码
     */
    @Getter
    private final String code;

    /**
     * 字段名称 - 对应元信息字段名
     */
    @Getter
    private final String name;

    /**
     * 名称
     */
    @Getter
    private final String text;

    LegIdType(String code, String name, String text) {
        this.code = code;
        this.name = name;
        this.text = text;
    }

    public static String getCodeByText(String text) {
        if (Strings.isNullOrEmpty(text)) {
            return null;
        }
        return Stream.of(values())
                .filter(legIdType -> legIdType.getText().equals(text))
                .findFirst()
                .map(LegIdType::getCode)
                .orElse(null);
    }

    public static LegIdType getByCode(String code) {
        if (SHXYM.getCode().equals(code)) {
            return SHXYM;
        } else if (GSZC.getCode().equals(code)) {
            return GSZC;
        } else if (ZZJG.getCode().equals(code)) {
            return ZZJG;
        } else if (SWDJ.getCode().equals(code)) {
            return SWDJ;
        } else if (SYDW.getCode().equals(code)) {
            return SYDW;
        } else if (SHZZ.getCode().equals(code)) {
            return SHZZ;
        } else {
            return OTHER;
        }
    }

    public static Set<LegIdType> get3Code() {
        return Sets.newHashSet(SHXYM, GSZC, ZZJG);
    }

    public static String getNameByCode(String code) {
        return getByCode(code).getName();
    }

    public static Map<String, Object> getNameByCode(String code, Object val) {
        Map<String, Object> map = Maps.newLinkedHashMap();
        Stream.of(values()).forEach(id -> map.put(id.getName(), null));
        map.put(getNameByCode(code), val);
        return map;
    }
}
